/**
 *
 * Created by luckyjing on 16/2/5.
 */
var User = require('mongoose').model('User');
var Util = require('../util/util');
var path = require('path');
//注册
exports.signup = function (req, res) {
    if (!req.user) {
        var user = new User(req.body);
        var message = null;
        user.provider = 'local';
        user.save(function (err) {
            console.log(err);
            if (err) {
                res.json(Util.Info.ERROR);
            } else {
                req.login(user, function (err) {
                    if (err) {
                        res.json({
                            message: "登录失败"
                        })
                    } else {
                        res.json({
                            message: "登陆成功",
                            status: 1000
                        })
                    }
                })
            }
        })
    }
};
exports.logout = function (req, res) {
    if (req.user) {
        req.logout();
        res.redirect('/');
    }
};
//注册页面
exports.renderSignup = function (req, res) {
    if (!req.user) {
        res.sendfile(path.join(__dirname, '../views/signup.html'));
    } else {
        return res.redirect('/');
    }
};
//登录页面
exports.renderSignin = function (req, res) {
    if (!req.user) {
        res.sendfile(path.join(__dirname, '../views/signin.html'));
    } else {
        res.redirect('/');
    }
};
/**
 * @description 增加用户
 * @param req
 * @param res
 * @param next
 */
exports.create = function (req, res, next) {
    var user = new User(req.body);
    console.log(user);
    // user.save(function (err) {
    //     if (err) {
    //         return next(err);
    //     } else {
    //         res.json(user);
    //     }
    // })
};
/**
 * @description 显示所有用户
 * @param req
 * @param res
 * @param next
 */
exports.list = function (req, res, next) {
    User.find({}, '-password -salt', function (err, users) {
        if (err) {
            return next(err);
        } else {
            res.json(users);
        }
    })
};
exports.listByEmail = function (req, res, next) {
    if (!req.query.email) {
        return res.json({errorMsg: 'email can not be null'});
    }
    User.findOneByEmail(req.query.email, function (err, user) {
        if (err) {
            return next(err);
        } else {
            res.json(user);
        }
    })
};
exports.read = function (req, res) {
    res.json(req.user);
};
// 将当前用户的信息返回给前台
exports.info = function (req, res) {
    if (req.user) {
        res.json(req.user);
    } else {
        res.redirect('/');
    }
};
/**
 * 中间件,获取当前用户并将其纳入request中,以供后序使用
 * @param req
 * @param res
 * @param next
 * @param id
 */
exports.userByID = function (req, res, next, id) {
    User.findOne({
        _id: id
    }, function (err, user) {
        if (err) {
            return next(err);
        } else {
            req.user = user;
            next();
        }
    })
};
exports.update = function (req, res, next) {
    User.findByIdAndUpdate(req.user.id, req.body, function (err, user) {
        if (err) {
            res.json(Util.Info.ERROR);
        } else {
            res.json(user);
        }
    })
};
exports.delete = function (req, res) {
    req.user.remove(function (err) {
        if (err) {
            res.json(Util.Info.ERROR);
        } else {
            res.json(Util.Info.SUCCESS);
        }
    })
};